package com.whut.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @ClassName:MyMetaObjectHandler
 * @Description:通用字段处理函数
 * @Author:liumengying
 * @Date: 2022/8/3 22:18
 * Version v1.0
 */
@Component    //加了该注解的类会被Spring框架扫描到并初始化到Bean对象中，在应用其他位置可以注入
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject){
        log.info("公共字段自动填充【insert】....");
        log.info(metaObject.toString());

        //为公共字段属性设置值
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]....");
        log.info(metaObject.toString());

        //公共字段属性设置值
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());

    }

}
